# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time        : 2021/10/16 09:02
@Author      : Albert Darren
@Contact     : 2563491540@qq.com
@File        : match_hist.py
@Version     : Version 1.0.0
@Description : TODO
@Created By  : PyCharm
"""
from skimage import exposure
from pydicom import dcmread
from DIP_experiment_5.dcm_io import bar
from DIP_experiment_5.util import contrast_show
im_path = "../experiment_fig/I1700000"
ref_path = "../experiment_fig/CT000009"
font_path = "C:/Windows/Fonts/simhei.ttf"
# 读取原始图像
im = dcmread(im_path)
im_arr = im.pixel_array  # 2395 0
# 读取参考图像
ref = dcmread(ref_path)
ref_arr = ref.pixel_array  # 3044 -2000
# 直方图匹配图像
match_im = exposure.match_histograms(im_arr, ref_arr)
# 显示原始图像、参考图像、匹配图像
fig_dict = {"DICOM原始图像": im_arr, "DICOM参考图像": ref_arr, "DICOM匹配图像": match_im}
contrast_show(fig_dict, (1, 3), font=font_path)
# 显示原始图像、参考图像和匹配图像直方图
im_height, im_bins = exposure.histogram(im_arr, normalize=True)
ref_height, ref_bins = exposure.histogram(ref_arr, normalize=True)
mat_height, mat_bins = exposure.histogram(match_im, normalize=True)
im_dict = {"DICOM原始图像直方图": [im_bins, im_height], "DICOM参考图像直方图": [ref_bins, ref_height],
           "DICOM匹配图像直方图": [mat_bins, mat_height]}
bar(im_dict, (1, 3), wspace=0.4, font=font_path)
